package com.funtl.my.shop.web.admin.abstracts;

import com.funtl.my.shop.domain.TbUser;
import com.futl.my.shop.commons.dto.PageInfo;
import com.futl.my.shop.commons.persistence.BaseEntity;
import com.futl.my.shop.commons.persistence.BaseTreeEntity;
import com.futl.my.shop.commons.persistence.BaseTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author tw
 * @version 1.0
 * @date 2020/6/2 21:33
 */
public abstract class AbstractBaseTreeController<T extends BaseTreeEntity, S extends BaseTreeService<T>>{

    @Autowired
    protected S service;

    public abstract String form(T entity);

    public abstract String list(Model model);

    public abstract String save(T entity, Model model, RedirectAttributes redirectAttributes);

    public abstract List<T> treeData(Long id);

    /**
     * 排序
     *
     * @param sourceList
     * @param targetList
     * @param parentId
     */
    protected void sortList(List<T> sourceList, List<T> targetList, Long parentId) {
        for (T sourceEntity : sourceList) {
            if (sourceEntity.getParent().getId().equals(parentId)) {
                targetList.add(sourceEntity);
                //判读有没有子节点,如果有继续追缴
                if (sourceEntity.getIsParent()) {
                    for (T currentEntity : sourceList) {
                        if (currentEntity.getParent().getId().equals(sourceEntity.getId())) {
                            sortList(sourceList, targetList, sourceEntity.getId());
                            break;
                        }
                    }
                }
            }
        }
    }
}
